﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Updates.aspx.cs" Inherits="P.InOut.NetMvc.Components.UpdatesComponent.Updates" %>
<%@ Import Namespace="P.InOut.NetMvc.Models" %>
<%@ Import Namespace="P.InOut.NetMvc.Common" %>

<script type="text/javascript">
    $(document).ready(function($) {
        $.each($("li.updateText"), function(index, object) {
            var updateText = object.html();
            var formattedText = formattedText(updateText);
            object.html(formattedText);
        });
    });

    function formatText(str) {

        return str.replace(/(\w+):\/\/[\S]+(\b|$)/gim,
                '<a href="$&" class="updateLink" target="_blank">$&</a>')
            .replace(/([^\/])(www[\S]+(\b|$))/gim,
                '$1<a href="http://$2" class="updateLink" target="_blank">$2</a>')
            .replace(/^(www[\S]+(\b|$))/gim,
                '<a href="http://$1" class="updateLink" target="_blank">$1</a>');

    }
</script>

<% DateTime date = new DateTime(); %>
<!-- // List each update in the ViewData -->
<ul id="teamUpdates">
    <% foreach (Update update in ViewData.Model.Updates) { %>
        
        <% if (date == null || date != update.DateTimeEntered.Date) { %>
        
            <% if (update.DateTimeEntered.Date == DateTime.Today.Date) { %>
                    
                    <li class="listheader">
                        <span style="background-color: Yellow; padding: 2px 1em 2px 2em;">
                            TODAY
                        </span>
                    </li>
                
            <% } %>
                    
            <% else if (update.DateTimeEntered.Date == DateTime.Today.Date.Subtract(new TimeSpan(1, 0, 0, 0))) { %>
                
                <li class="listheader">
                    <span style="padding: 2px 1em 2px 5px; text-decoration: underline;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        YESTERDAY
                    </span>
                </li>
                
            <% } %>
            
            <% else { %>
            
                <li class="listheader">
                    <span style="padding: 2px 1em 2px 2px; text-decoration: underline;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <%= Utilities.GetFormattedDate(update.DateTimeEntered) %>
                    </span>
                </li>
            
            <% } %>
        
        <% } %>
        
        <li class="update">
            <span style="font-weight: bold;">
                <%= Html.ActionLink(
                        update.User.Profile.FirstName == null ||
                        update.User.Profile.FirstName == string.Empty ?
                        update.User.Aspnet_User.UserName :
                        update.User.Profile.FirstName + " "
                        + update.User.Profile.LastName, 
                        "Detail", "Updates", new { id = update.Aspnet_Membership.UserId }) %>:
            </span>
            <span id="<%= update.Id %>" class="updateText">
                <%= P.InOut.NetMvc.Common.Utilities.FormatText(update.UpdateText) %>
            </span>
        
            <% if((Guid)Membership.GetUser().ProviderUserKey == update.UserId && DateTime.Now < update.DateTimeEntered.AddHours(2)) { %>
        
            
                <a style="font-size: 0.75em;" id="editLink<%= update.Id %>" 
                    href="javascript:editUpdate(<%= update.Id %>)">edit</a>
                <span id="edit<%= update.Id %>" style="font-size: 0.75em; display: none;">
                    <% using (Html.Form<P.InOut.NetMvc.Controllers.UpdatesController>(action => action.Update(update.Id), FormMethod.Post, new { style="display: inline;" }))
                       { %>
                        
                        <%= Html.TextBox("UpdateText", update.UpdateText) %>
                        <a href="javascript:cancelEdit(<%= update.Id %>)">cancel</a> 
                        <%= Html.ActionLink("delete", "Delete", "Updates" ,  new { id=update.Id }) %>
                    <% } %>
                </span>
            
            <% } %>
                
            <span class="timeelapsed"> 
            
                <% if (update.DateTimeEntered.Date >= DateTime.Today.Date.Subtract(new TimeSpan(8, 0, 0)))
                   { %>
                    <span id="elapsed<%= update.Id %>" class="elapsed">
                        - ▲ <%= Utilities.GetTimeElapsed(update.DateTimeEntered)%>
                    </span>                    
                <% } %>
            </span>
        </li>
        
        <% date = update.DateTimeEntered.Date; %>
        
    <% } %>
</ul>